<template>
    <a-modal v-model:open="visible" @ok="addIndex" :title="title">
        <a-divider>基本信息</a-divider>
        <a-row align="middle" :gutter="[5, 10]">
            <a-col :span="4"> 网站组 </a-col>
            <a-col :span="8">
                <a-select
                    :options="websiteGroupOptions"
                    v-model:value="indexParams.website_group_id"
                    show-search
                    style="width: 80%"
                    :filter-option="filterOption"
                ></a-select>
            </a-col>
            <a-col :span="4"> 负责人 </a-col>
            <a-col :span="8">
                <a-select
                    :options="userOptions"
                    show-search
                    style="width: 80%"
                    :filter-option="filterOption"
                    v-model:value="indexParams.create_id"
                ></a-select>
            </a-col>
            <a-col :span="4"> 行业类型 </a-col>
            <a-col :span="8">
                <a-select
                    :options="websiteTypeOptions"
                    v-model:value="indexParams.trade_type_id"
                    show-search
                    style="width: 80%"
                    :filter-option="filterOption"
                ></a-select>
            </a-col>
            <a-col :span="4"> 伪原创类型 </a-col>
            <a-col :span="8">
                <a-select
                    :options="artWycOptions"
                    style="width: 80%"
                    v-model:value="indexParams.wyc_type"
                    show-search
                    :filter-option="filterOption"
                ></a-select>
            </a-col>
            <template v-if="handleType == 3 && dataInfo.is_outside == 1">
                <a-col :span="4"> 索引类型 </a-col>
                <a-col :span="8">
                    <a-select v-model:value="indexPrefix" style="width: 80%" :options="indexTypeOptions"> </a-select>
                </a-col>
            </template>
        </a-row>
        <a-divider>索引信息</a-divider>
        <template v-if="handleType !== 3">
            <a-input type="text" placeholder="输入索引名" v-model:value="indexParams.index_name" v-if="handleType === 1">
                <template #addonBefore>
                    <a-select v-model:value="indexParams.es_host" style="width: 130px" :options="esHostOptions"> </a-select>
                </template>
                <template #addonAfter>
                    索引类型
                    <a-select v-model:value="indexPrefix" style="width: 90px" :options="indexTypeOptions"> </a-select>
                </template>
            </a-input>
            <a-row v-if="handleType === 2" align="middle" :gutter="[5, 10]">
                <a-col :span="4"> ES_HOST </a-col>
                <a-col :span="8">
                    <a-select
                        show-search
                        :filter-option="filterOption"
                        v-model:value="indexParams.es_host"
                        style="width: 80%"
                        :options="esHostOptions"
                    >
                    </a-select>
                </a-col>
                <a-col :span="4"> 选择索引 </a-col>
                <a-col :span="8">
                    <a-select
                        show-search
                        :filter-option="filterOption"
                        style="width: 80%"
                        v-model:value="indexParams.index_name"
                        :options="esServerIndexList"
                    >
                    </a-select>
                </a-col>
                <a-col :span="4"> 索引类型 </a-col>
                <a-col :span="8">
                    <a-select v-model:value="indexPrefix" style="width: 80%" :options="indexTypeOptions"> </a-select>
                </a-col>
                <a-col :span="4"> 公共索引 </a-col>
                <a-col :span="8">
                    <a-switch
                        v-model:checked="addIndexisCommon"
                        style="width: 80"
                        checked-children="是"
                        un-checked-children="否"
                    />
                </a-col>
            </a-row>
        </template>
        <a-input type="text" style="margin-top: 10px" placeholder="输入索引备注" v-model:value="indexParams.index_description">
            <template #addonAfter v-if="handleType === 1 || handleType === 3">
                <a-col :span="8">
                    公共索引
                    <a-switch
                        v-model:checked="addIndexisCommon"
                        style="width: 80"
                        checked-children="是"
                        un-checked-children="否"
                    />
                </a-col>
            </template>
        </a-input>
    </a-modal>
</template>

<script lang="ts" setup>
    import { isExistIndex } from "@/hooks/useIndexManagement";
    import { useCommonStaticOptions } from "@/hooks/useEsArticleCommon";
    import indexTypeOptions from "@/assets/json/indexType.json";
    import { useVModels } from "@vueuse/core";
    import esServerIndexFieldStore from "@/stores/esServerIndexField";
    import mainStore from "@/stores/main";
    import { message } from "ant-design-vue";
    import { indexManagementApi } from "@/api/esArticle";
    const $mainStore = mainStore();
    // handleType 1 新建 2 导入 3 更新
    type Props = {
        handleType: 1 | 2 | 3;
        visible: boolean;
        dataInfo: IndexManagementItem | Record<string, any>;
        userOptions: SelectOption;
        websiteGroupOptions: SelectOption;
        websiteTypeOptions: SelectOption;
        artWycOptions: SelectOption;
    };
    type Emits = {
        (event: "update:visible"): void;
        (event: "reload"): void;
    };
    // 添加索引是否为公共索引
    const addIndexisCommon = ref(false);
    const title = computed(() => (props.handleType == 3 ? "更新索引" : props.handleType == 2 ? "导入索引" : "新建索引"));
    const props = defineProps<Props>();
    const emits = defineEmits<Emits>();
    const $store = esServerIndexFieldStore();
    const esServerIndexMap = toRef($store, "esServerIndexMap");
    const esServerIndexList = computed(() => {
        return esServerIndexMap.value[indexParams.es_host];
    });
    const { visible } = useVModels(props, emits);
    const { filterOption, esHostOptions } = useCommonStaticOptions();
    const addIndex = () => {
        // 验证某些数据是否为空
        const isNotFields = ["website_group_id", "trade_type_id", "create_id"];
        for (const key of isNotFields) {
            // @ts-ignore
            if (indexParams[key] === 0) {
                message.error("请填写完整!");
                return;
            }
        }
        indexParams.is_common = addIndexisCommon.value ? 1 : 0;
        indexParams.is_outside = props.handleType === 2 ? 1 : 0;
        const obj = JSON.parse(JSON.stringify(indexParams));
        if (props.handleType === 2 || props.handleType === 1) {
            obj.index_type = indexPrefix.value;
            if (props.handleType === 1) {
                obj.index_name = obj.index_type + obj.index_name;
            }
            if (isExistIndex(obj.es_host, obj.index_name) && props.handleType === 1) {
                return message.error("索引已经存在!");
            }
            indexManagementApi(obj, "post")
                .then((res) => {
                    if (res.data.code === 200) {
                        if (props.handleType === 1) {
                            // @ts-ignore 添加到缓存中
                            $store.esServerIndexMap[obj.es_host].push({ label: obj.index_name, value: obj.index_name });
                        }
                        message.success("添加成功!");
                    }
                    emits("reload");
                    visible.value = false;
                })
                .catch((er) => {
                    message.error(er.data.msg);
                });
        }
        if (props.handleType === 3) {
            // 更新数据
            const _ = ["uid", "index_name", "es_host", "is_outside", "create_id"].map((key) => Reflect.deleteProperty(obj, key));
            obj["id"] = props.dataInfo.id;
            obj.index_type = indexPrefix.value;
            indexManagementApi(obj, "put")
                .then((res) => {
                    if (res.data.code === 200) {
                        message.success("修改成功!");
                    }
                    emits("reload");
                    visible.value = false;
                })
                .catch((er) => {
                    message.error(er.data.msg);
                });
        }
    };
    const indexPrefix = ref("article_");
    const indexParams = reactive<AddIndexManagement>({
        create_id: 0,
        trade_type_id: 0,
        website_group_id: 0,
        index_name: "",
        index_description: "",
        index_type: "",
        is_outside: 0,
        es_host: "http://192.168.1.92:9200",
        is_common: 0,
        wyc_type: "",
        uid: computed<number>(() => $mainStore.userInfo.id).value
    });

    onMounted(() => {
        if (props.handleType === 3) {
            for (const key in props.dataInfo) {
                if (key in indexParams) {
                    // @ts-ignore
                    indexParams[key] = props.dataInfo[key];
                }
            }
            indexPrefix.value = props.dataInfo.index_type;
        }
    });
</script>

<style scoped lang="scss"></style>
